package WeChat;

import com.mulanweb.db.common.Constant;
import com.mulanweb.db.common.WxParaFormatter;
import com.mulanweb.utils.RandomUtil;
import com.mulanweb.utils.WxSignUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.xml.sax.SAXException;

import javax.net.ssl.SSLContext;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Created by Administrator on 2016/11/17 0017.
 */
public class SellerPay {

    public static void main(String[] args) throws Exception{
        testPayUser();
    }

    public static void testPayUser() throws Exception{
        String url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";

        SortedMap<Object,Object> para = new TreeMap<Object,Object>();
        para.put("mch_appid", Constant.WeChatFinalValue.APPID);
        para.put("mchid",Constant.WeChatFinalValue.MCHID);
        para.put("nonce_str","orange");
        para.put("partner_trade_no", RandomUtil.createRandomOrderNumber());
        para.put("openid","oULNBwOvt_D2A_8se1bQOQ06ky0w");
        para.put("check_name","OPTION_CHECK");
        para.put("re_user_name","曾德仁");
        para.put("amount","100");
        para.put("desc","企业API支付测试");
        para.put("spbill_create_ip","121.199.77.244");
        String sign  = WxSignUtil.createGetPrepayIdSign(para);
        para.put("sign",sign);

        String xml = WxParaFormatter.getRequestXml(para);

        //指定格式读取证书格式为PKCS12
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        //读取本机存放的证书文件
        FileInputStream instream = new FileInputStream(new File("D:/apiclient_cert.p12"));
        try {
            //指定PKCS12的密码(商户ID)
            keyStore.load(instream, "1377954502".toCharArray());
        } finally {
            instream.close();
        }

        // Trust own CA and all self-signed certs
        SSLContext sslcontext = SSLContexts.custom()
                .loadKeyMaterial(keyStore, "1377954502".toCharArray())
                .build();
        // Allow TLSv1 protocol only
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslcontext,
                new String[]{"TLSv1"},
                null,
                SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        CloseableHttpClient httpclient = HttpClients.custom()
                .setSSLSocketFactory(sslsf)
                .build();


        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        post.addHeader("Content/Type", "application/xml");
        StringEntity stringEntity = new StringEntity(xml, ContentType.create("application/xml", "UTF-8"));
        post.setEntity(stringEntity);
        CloseableHttpResponse resp = null;
        try {
            resp =  httpclient.execute(post);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int returnCode = resp.getStatusLine().getStatusCode();
        String str = "";
        if (200 <= returnCode && 300 >= returnCode) {
            try {
                str = (EntityUtils.toString(resp.getEntity()));
                Map<String, String> result = WxParaFormatter.xmlToMap(str);
                System.out.println(result);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
        }
    }


}
